package cn.topcodes.tcsf.app.web.controller.userend;

import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.commons.domain.Page;
import cn.topcodes.tcsf.app.domain.entity.AppUser;
import cn.topcodes.tcsf.app.domain.entity.Notice;
import cn.topcodes.tcsf.app.service.AppUserService;
import cn.topcodes.tcsf.app.service.NoticeService;
import cn.topcodes.tcsf.app.web.controller.userend.interceptor.IgnoreUserendAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户后台
 */
@Controller
@RequestMapping("/userend")
public class UserEndController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private NoticeService noticeService;

    /**
     * 登录页
     * @return
     */
    @IgnoreUserendAuth
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        return "userend/login";
    }

    /**
     * 主页
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpSession session, Model model) {
        String token = (String) session.getAttribute("token");
        AppUser appUser = appUserService.findByToken(token);
        model.addAttribute("appUser", appUser);
        return "userend/index";
    }

    @RequestMapping(value = "home", method = RequestMethod.GET)
    public String home(Model model) {
        Notice notice = noticeService.getLastNotice();
        model.addAttribute("notice", notice);
        return "userend/home";
    }

    /**
     * 个人信息页
     * @return
     */
    @RequestMapping(value = "person-info", method = RequestMethod.GET)
    public String personInfo(HttpSession session, Model model) {
        String token = (String) session.getAttribute("token");
        AppUser appUser = this.appUserService.findByToken(token);
        model.addAttribute("appUser", appUser);
        return "userend/person-info";
    }

    /**
     * 修改密码页
     * @return
     */
    @RequestMapping(value = "modify-password", method = RequestMethod.GET)
    public String modifyPassword() {
        return "userend/modify-password";
    }

    /**
     * 系统公告列表页
     * @return
     */
    @RequestMapping(value = "notices", method = RequestMethod.GET)
    public String notices() {
        return "userend/notices";
    }

    /**
     * 系统公告列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "notices/list", method = RequestMethod.GET)
    public ApiResponseEntity<Page<Notice>> notices(
            @RequestParam(name = "page", defaultValue = "1", required = false) Integer pageNumber,
            @RequestParam(name = "size", defaultValue = "20", required = false) Integer pageSize
    ) {
        Page<Notice> notices = this.noticeService.findPageBy(pageNumber, pageSize);
        return ApiResponseEntity.buildOK(notices);
    }
}
